1. /* sxfxsmul.cpp by K.Tsuru */
  2. // function ID = 509 BRADIX
  3. /*******************************************************************
  4. SDecimal class
  5. Provides the multiplication of SDecimal and a short integer together.
  6. result = m*x, x <= ULONG_MAX/BRADIX
  7. ********************************************************************/
  8. #ifndef SN_H
  9. #include "sn.h"
  10. #endif
  11. static const char* const func = "XsMult";
  12. void XsMult(const SDecimal& m, ulong x, SDecimal& result){
  13. if( !m.Sign() || (x == 0) ){
  14. result.SetZero(); return;
  15. }
  16. if(x == 1){ result = m; return; }
  17. if(x > m.SlOpMaxValue()) m.SetError(m.OUT_OF_RANGE, func, 509);
  18. if( result.figure.size() != m.figure.size() ) m.SetError(m.SYNTAX_ERR, func, 509);
  19. uint rh = m.aHead, rt = m.aTail;
  20. fType* rv = result.figure.Elements();
  21. const fType* mv = m.ReadFigures();
  22. #ifndef NDEBUG
  23. result.figure[rh];
  24. #endif
  25. ulong w = 0;
  26. //"rt" must be casted as int considering the case of rt==0.
  27. int i;
  28. for(i = (int)rh; i >= (int)rt; i--) {
  29. w += (ulong)mv[i] * x;
  30. rv[i] = fType(w & BRADIX1); // = w%BRADIX
  31. w >>= BRADIX_BITS; // w /= BRADIX;
  32. }
  33. if(w){ //It proceeds the carry. It can be possible that w >= BRADIX.
  34. for( ; w && (i >= 0) ; i--){
  35. rv[i] = fType(w & BRADIX1);
  36. w >>= BRADIX_BITS;
  37. }
  38. if(w) m.SetError(m.OVERFLOW_ERR, func, 509);
  39. rt = i+1;
  40. }
  41. //Clear the outside of the above roop.
  42. if(result.aHead > rh) result.figure.clear(rh+1, result.aHead);
  43. if(result.aTail < rt) result.figure.clear(result.aTail, rt-1); // rt > 0
  44. //It decides the figure position.
  45. result.aTail = rt;
  46. while(!rv[rh]) rh--;
  47. result.aHead = rh;
  48. result.SetSign( m.Sign() ); // result != 0
  49. #ifndef NDEBUG
  50. result.figure(rt); result.figure(rh);
  51. #endif
  52. }

sxfxsmul.cpp : last modifiled at 2017/03/13 14:32:02(1,741 bytes)
created at 2015/12/22 16:09:56
The creation time of this html file is 2017/10/27 15:45:59 (Fri Oct 27 15:45:59 2017).